package cn.liangcaomei.eduservice.service.impl;

import cn.liangcaomei.eduservice.entity.EduCourse;
import cn.liangcaomei.eduservice.entity.EduCourseDescription;
import cn.liangcaomei.eduservice.entity.EduTeacher;
import cn.liangcaomei.eduservice.entity.frontvo.CourseFrontVo;
import cn.liangcaomei.eduservice.entity.frontvo.CourseWebVo;
import cn.liangcaomei.eduservice.entity.vo.CourseInfoVo;
import cn.liangcaomei.eduservice.entity.vo.CoursePublishVo;
import cn.liangcaomei.eduservice.mapper.EduCourseMapper;
import cn.liangcaomei.eduservice.service.EduChapterService;
import cn.liangcaomei.eduservice.service.EduCourseDescriptionService;
import cn.liangcaomei.eduservice.service.EduCourseService;
import cn.liangcaomei.eduservice.service.EduVideoService;
import cn.liangcaomei.servicebase.exceptionhandler.CustException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author LiuJie
 * @since 2024-01-27
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //课程描述注入
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    //小节注入
    @Autowired
    private EduVideoService eduVideoService;

    //章节注入
    @Autowired
    private EduChapterService eduChapterService;

    //添加課程信息方法
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 添加課程表信息
        // courseInfoVo 對象轉換 eduCourse 對象
        EduCourse eduCoutse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCoutse);
        int insert = baseMapper.insert(eduCoutse);
        if (insert == 0) {
            //add error
            throw new CustException(2001,"添加課程信息失敗");
        }

        //獲取添加后的課程信息記錄的id
        String cid = eduCoutse.getId();

        //2 添加課程表簡介表信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(cid);
        eduCourseDescriptionService.save(eduCourseDescription);

        return cid;
    }

    //根据课程id，查询课程信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
//        System.out.println(eduCourse);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2 查询课程描述表
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1 修改 课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int updateList = baseMapper.updateById(eduCourse);
        if (updateList == 0) {
            throw new CustException(20001,"课程信息修改失败");
        }

        //2 修改 课程描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(courseDescription);
    }

    //根据课程id查询课程的确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用Mapper(自己写的只能用 baseMapper 调)
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //1 删除小节
        eduVideoService.removeVideoCourseId(courseId);

        //2 删除章节
        eduChapterService.removeChapterCourseId(courseId);

        //3 删除描述
        eduCourseDescriptionService.removeById(courseId);

        //4 删除课程
        int result = baseMapper.deleteById(courseId);
        if (result == 0) {
            throw new CustException(20001,"删除失败");
        }
    }

    //1 条件查询带分页前台
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
            wrapper.eq("subject_parent_id ",courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
            wrapper.eq("subject_id ",courseFrontVo.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageCourse,wrapper);

        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();

        HashMap<String, Object> map = new HashMap<>();

        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //2 查询课程的详情信息方法
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
